﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace The_LarX
{
    public class Node_Object : VisualScript_Node, IVisualScriptNode_Object
    {
        public Node_Object() { Load(); }

        public Node_Object(VisualScript Script)
            : base(Script) { Load(); }

        private void Load()
        {
            this.descriptionName = "";
            this.RepresentationType = typeof(VSR_Object);
        }

        private INameable obj;
        public INameable Object
        {
            get { return obj; }
            set 
            {
                if (value != null)
                {
                    obj = value;
                    this.descriptionName = obj.Name;
                }
            }
        }

        public override void SerializeData(SerializeData Data)
        {
            base.SerializeData(Data);

            string objName = "";
            string objType = "";
            if (Object is Actor)
            {
                Actor obj = Object as Actor;
                objName = obj.Name;
                objType = obj.GetType().FullName;
            }

            Data.AddData(CreatedNumber + "ObjectName", objName);
            Data.AddData(CreatedNumber + "ObjectType", objType);
        }
        public override void DeserializeData(SerializeData Data)
        {
            base.DeserializeData(Data);

            string objName = Data.GetData<string>(CreatedNumber + "ObjectName");
            string objType = Data.GetData<string>(CreatedNumber + "ObjectType");
            Type type = Type.GetType(objType);

            if (type == typeof(Actor) ||
                type.BaseType == typeof(Actor))
            {
                Object = EngineSystem.Actor_ByName(objName);
            }
        }
    }
}
